using Nekonya.Dicts;
using Nekonya.Dicts.Model;
using Nekonya.Managers;
using TinaX;
using UnityEngine;

namespace Nekonya.Components
{
    /// <summary>
    /// 范围 可拾取组件
    /// </summary>
    [RequireComponent(typeof(Collider))]
    public class RangePickable : MonoBehaviour
    {
        [Header("道具ID")]
        public int Id;

        [Header("自动拾取")]
        [Tooltip("玩家碰到之后会自动拾取")]
        public bool AutoPick = true;

        public ItemModel ItemModel { get; private set; }
        public ItemDict ItemDict { get; private set; }

        private BagManager m_BagManager;


        private void Awake()
        {
            if (XCore.MainInstance == null)
                return;
            var dictFactory = XCore.MainInstance?.GetService<DictFactory>();
            ItemDict = dictFactory.GetDict<ItemDict>();
            ItemModel = ItemDict.Get(this.Id);
            m_BagManager = XCore.MainInstance.Services.Get<BagManager>();

            //Debug.Log("道具:" + ItemDict.GetName(ItemModel));
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.GetComponent<PlayerCharacterController>())
            {
                //说明碰到了玩家
                if (AutoPick)
                {
                    m_BagManager.AddItem(this.Id, 1);
                    gameObject.Destroy();
                }
            }
        }

    }
}
